/*
 * Copyright 2009 Gal Dolber
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.google.gwt.user.client.ui;

import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.ui.HTMLTable.CellFormatter;
import com.google.gwt.user.client.ui.HTMLTable.RowFormatter;
import com.google.gwt.user.client.ui.UserAgent.Agents;

public class RoundedPanel extends Composite {

	/**
	 * The default style name.
	 */
	private static String DEFAULT_STYLENAME = "Dimensionable";

	protected Grid grid = new Grid(3, 3);
	protected CellFormatter cellFormatter = grid.getCellFormatter();
	protected RowFormatter rowFormatter = grid.getRowFormatter();

	public RoundedPanel() {
		this(DEFAULT_STYLENAME);
	}

	public RoundedPanel(String styleName) {
		grid.setCellPadding(0);
		grid.setCellSpacing(0);

		initWidget(grid);

		String[] rowStyles = { "Dtop", "Dmiddle", "Dbottom" };
		String[] colStyles = { "Left", "Center", "Right" };

		// Add each row
		for (int x = 0; x < 3; x++) {
			for (int y = 0; y < 3; y++) {
				String cellStyle = rowStyles[y] + colStyles[x];

				Element e = cellFormatter.getElement(y, x);
				e.setClassName(cellStyle);
			}
		}

		for (int i = 0; i < 3; i++) {
			rowFormatter.getElement(i).setClassName(rowStyles[i]);
		}

		// Safari line-height problem
		if (UserAgent.getUserAgent() == Agents.SAFARI) {
			rowFormatter.getElement(0).getStyle().setProperty("line-height",
					"0px");
			rowFormatter.getElement(2).getStyle().setProperty("line-height",
					"0px");
		}

		setStyleName(styleName);
	}

	public Widget getWidget() {
		return grid.getWidget(1, 1);
	}

	public void setWidget(Widget widget) {
		grid.setWidget(1, 1, widget);
	}
}
